package com.keyinong.parkin.home.adapter;

import java.util.ArrayList;

import com.example.parkin.R;
import com.example.parkin.R.drawable;
import com.keyinong.parkin.home.bean.ParkServiceBean;
import com.keyinong.parkin.util.ImageUtils;
import com.keyinong.parkin.util.ImageUtilsLoaderFile;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeServiceAdapter extends BaseAdapter {
	Context context;
	ArrayList<ParkServiceBean> list;
	int servicecolo[] = { R.color.homeBlue, R.color.homeRed, R.color.homeyellow };
    
	
	public HomeServiceAdapter(Context context, ArrayList<ParkServiceBean> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHandle handle;
		if (view == null) {
			handle = new ViewHandle();
			view = LayoutInflater.from(context).inflate(
					R.layout.item_grid_homeservice, null);
			handle.img_service = (ImageView) view
					.findViewById(R.id.img_service);
			handle.tv_serviceName = (TextView) view
					.findViewById(R.id.tv_serviceName);
			view.setTag(handle);
		} else {
			handle = (ViewHandle) view.getTag();
		}
		setDefaultIcom(handle,position);
		
		return view;
	}

	private class ViewHandle {
		ImageView img_service;
		TextView tv_serviceName;
	}
	
	private void setDefaultIcom(ViewHandle handle,int position){
		int serviceid=list.get(position).getId();
		if(list.get(position).getName()!="" ||serviceid!=0){
			
			ImageUtils.displayImage(context, list.get(position).getLogo(),
					handle.img_service, returnImagRes(serviceid));
//			Log.e("lh", "logo--"+list.get(position).getLogo());
//			handle.tv_serviceName.setTextColor(context.getResources().getColor(servicecolo[position%servicecolo.length]));
			handle.tv_serviceName.setText(list.get(position).getName());
		}
	}
	private int returnImagRes(int serviceid){
		if(serviceid==1){
			return R.drawable.img_home_paring;
		}else if(serviceid==2){
			return R.drawable.img_home_meenting;
		}else if(serviceid==3){
			return R.drawable.img_home_ticket;
		}else if(serviceid==4){
			return R.drawable.img_home_booth;
		}
		return R.drawable.img_home_paring;
	}

}
